package org.jcut.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.validation.Valid;

import org.jcut.bean.Shop;
import org.jcut.data.Data;
import org.jcut.service.ShopService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.Errors;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/shop")
public class ShopController {
	@Value("${imgsPath}")
	private String path;
	@Autowired
	private ShopService service;
	@Autowired
	private Data data;
	@RequestMapping("/selectByid")
	public Object selectByid(@RequestParam("id")String id) {
	List<Shop> shop=new ArrayList<Shop>();
	shop.add(service.selectByPrimaryKey(id));
	System.out.println(id);
	data.setMyData(1, "查询成功",shop );	
	return data;
	}
	/**
	 * @author 王征
	 * @param introduce 商品介绍
	 * @param shopname  商品名称
	 * @param tel       摊主联系方式
	 * @param uid		用户id
	 * @param myImg     摊点图片
	 * */
	@RequestMapping("/add")
	public Object add(@Valid Shop shop,@RequestParam(value="myImg",required =false) MultipartFile file,Errors error)  {
		List<ObjectError> e=error.getAllErrors();
		System.out.println(e.size());
		
		if(e.size()>0) {
			for(ObjectError ee:e) {
				data.setMyData(0, ee.getDefaultMessage(), "0");
				break;
			}
			
		}else {
			String imgName=UUID.randomUUID().toString();
			try {
				
				String imgPath=path+imgName+".jpg";
				file.transferTo(new File(imgPath));
				
				shop.setImg("imgs/"+imgName+".jpg");
			} catch (Exception j) {
				// TODO Auto-generated catch block
				j.printStackTrace();
			} 
			shop.setId(UUID.randomUUID().toString());
			int i=service.insert(shop);
			
			data.setMyData(i, i>0?"添加商品成功":"服务器异常", i);
			
		}
		return data;
	
	}
	/**
	 * @author 王征
	 * @param x  地图x轴
	 * @param y  地图y轴
	 * @param id 摊点id
	 * 
	 * */
	@RequestMapping("/addLocation")
	public Object addLocation(Shop shop) {
		int i=service.addLocation(shop);
		data.setMyData(i, i>0?"添加位置完成":"服务器异常", i);
		return data;
	}
	/**
	 * 
	 * @author 王征
	 * @param dis 显示状态
	 * @param id  摊点id
	 * */
	@RequestMapping("/updateDis")
	public Object updateDis(Shop shop) {
		int i=service.updateDis(shop);
		data.setMyData(i, i>0?"修改状态完成":"服务器异常", i);
		return data;
	}
	/**
	 * @author 王征
	 * @param uId 摊主id
	 * */
	@RequestMapping("/selectByUidShop")
	public Object selectByUidShop(@RequestParam("uId")String uId,@RequestParam(value="pageNum",required = false)String pageNum,@RequestParam(value="pageSize",required = false) String pageSize) {
		if ((pageNum != null && !("".equals(pageNum))) && (pageSize != null && !("".equals(pageSize)))) {
			PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
		}
		PageInfo<Shop> pageInfo = new PageInfo<>(service.selectByUidShop(uId));
		data.setMyData(1, "查询成功", pageInfo);
		return data;
	}
	/**
	 * @author 王征
	 * @param x 地图x坐标
	 * @param y 地图y坐标
	 * @return id 返回摊点的id
	 * */
	@RequestMapping("/selectXYbyShop")
	public Object selectXYbyShop(Shop shop) {
		if(shop.getX()!=null&&!("".equals(shop.getX()))&&shop.getY()!=null&&!("".equals(shop.getY()))) {
			String id=service.selectXYbyShop(shop);
			data.setMyData(1,"查询成功",id);
		}else {
			data.setMyData(0,"坐标不能为空",0);
		}
		return data;
		
	}
	@RequestMapping("/selectAll")
	public Object selectAll() {
		data.setMyData(1, "查询成功", service.selectAll());
		
		return data;
	}
	/**
	 * @author 王征
	 * @param id 商品id
	 * */
	@RequestMapping("/selectByCid")
	public Object selectByCid(@RequestParam("cid")String cid) {
		data.setMyData(1, "查询成功", service.selectByCid(cid));
		return data;
	}
	/**
	 * @author 王征
	 * @param  name 小摊名称 模糊查询
	 * 
	 * 
	 * */
	@RequestMapping("/selectByName")
	public Object selectByName(String name) {
		data.setMyData(1, "查询成功", service.selectByName(name));
		return data;
	}

	

}
